using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace PeDALS.Utilities
{
    public static class XmlHelper
    {
        public static string GetAnyTypeValue(XmlDocument doc, string xPath)
        {
            XmlNode node = doc.SelectSingleNode(xPath);

            return node.InnerText;
        }

        public static string GetNZMEFileData(XmlDocument doc, string xPath, string nodeName)
        {
            XmlNode node = null;

            XmlElement root = doc.DocumentElement;
            node = root.SelectSingleNode(xPath, null);
            doc = new XmlDocument();
            if (null != node)
                doc.LoadXml(node.OuterXml);

            node = null;

            switch (nodeName)
            {
                case "Size":
                    xPath = @"/*[local-name()='File' and namespace-uri()='']/*[local-name()='Size' and namespace-uri()='']";
                    node = doc.SelectSingleNode(xPath);
                    if (node != null)
                        return node.InnerText;
                    else
                        return "";
                case "Extension":
                    xPath = @"/*[local-name()='File' and namespace-uri()='']/*[local-name()='Filename' and namespace-uri()='']/*[local-name()='Extension' and namespace-uri()='']";
                    node = doc.SelectSingleNode(xPath);
                    if (node != null)
                        return node.InnerText;
                    else
                        return "";
                case "Format":
                    xPath = @"/*[local-name()='File' and namespace-uri()='']/*[local-name()='FileFormat' and namespace-uri()='']/*[local-name()='Format' and namespace-uri()='']";
                    node = doc.SelectSingleNode(xPath);
                    if (node != null)
                        return node.InnerText;
                    else
                        return "";
                default:
                    return "";
            }
        }

        public static XmlDocument CreateAIPWithNZMEData(XmlDocument msg, string xpath, string size, string extension, string name)
        {
            XmlNode node = null;

            XmlElement root = msg.DocumentElement;
            node = root.SelectSingleNode(xpath, null);

            XmlAttribute attSize = msg.CreateAttribute("FileSize");
            attSize.Value = size;
            node.Attributes.Append(attSize);

            XmlAttribute attformat = msg.CreateAttribute("DigitalFormatFileType");
            attformat.Value = extension;
            node.Attributes.Append(attformat);

            XmlAttribute attExtension = msg.CreateAttribute("DigitalFormatRenderingSoftwareName");
            attExtension.Value = name;
            node.Attributes.Append(attExtension);

            return msg;
        }

        public static void StreamSuperPackageWithEnvelope(string superPackageId, string superPackagePath, string superPackageTempPath)
        {
            if(File.Exists(superPackagePath))
            {
                File.Delete(superPackagePath);
            }

            using(FileStream fsWrite = File.Create(superPackagePath))
            {
                byte[] header = new UTF8Encoding(true).GetBytes("<ns0:SuperPackage xmlns:ns0=\"http://PeDALS\" id=\"" + superPackageId + "\"><AIPs>");
                byte[] footer = new UTF8Encoding(true).GetBytes("</AIPs></ns0:SuperPackage>");

                fsWrite.Write(header, 0, header.Length);

                using(FileStream fsRead = File.OpenRead(superPackageTempPath))
                {
                    byte[] b = new byte[4096];
                    int byteLength = fsRead.Read(b,0,b.Length);

                    while(byteLength > 0)
                    {
                        fsWrite.Write(b,0,byteLength);
                        byteLength = fsRead.Read(b, 0, b.Length);
                    }
                }

                fsWrite.Write(footer, 0, footer.Length);
            }
        }

        public static string GetStringNotNull(string str)
        {
            return (str == null ? "" : str);
        }

        public static int GetIntNotNull(string str)
        {
            int res;
            bool ret = int.TryParse(str, out res);

            return (ret ? res : 0);
        }

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="xpath">The xpath.</param>
        /// <returns></returns>
        public static int GetNodeCount(XmlDocument msg, string xpath)
        {
            XmlNodeList nodeList;
            int i = 0;

            nodeList = msg.SelectNodes(xpath);
            i = nodeList.Count;

            return i;
        }

        /// <summary>
        /// Gets the node as XML.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="xpath">The xpath.</param>
        /// <returns></returns>
        public static XmlDocument GetNodeAsXML(XmlDocument msg, string xpath)
        {
            XmlNode node = null;

            XmlElement root = msg.DocumentElement;
            node = root.SelectSingleNode(xpath, null);
            msg = new XmlDocument();
            if (null != node)
                msg.LoadXml(node.OuterXml);

            node = null;
            return msg;
        }
    }
}
